//
//  YIMOSS.swift
//  YIM
//
//  Created by YMac on 2024/8/4.
//

import AliyunOSSiOS

internal class qkQorSShcjrKyhkX: NSObject {
    var mNmhAdVrLoDmLYjv: OSSClient?
    var raHxkMJGVZaVKyRg = ""
    var xdddztrOGitTYeMv: String?
    
    func mBcRiYEyhdTTBqyF(EeqbYNIBaZfkqYaF: [String: Any]?) {
        if EeqbYNIBaZfkqYaF == nil { return }
        raHxkMJGVZaVKyRg = EeqbYNIBaZfkqYaF!["bucketName"] as! String
        xdddztrOGitTYeMv = EeqbYNIBaZfkqYaF!["cdnPath"] as? String
        let OXknxPAOlgZvdwIV = EeqbYNIBaZfkqYaF!["ossEndpoint"] as! String
        let qiFnpqkgnIMowCCr = EeqbYNIBaZfkqYaF!["accessKey"] as! String
        let LSCrLFneQOQTFpEp = EeqbYNIBaZfkqYaF!["accessSecret"] as! String
        let MgMtRBePMHRwscie = EeqbYNIBaZfkqYaF!["secureToken"] as! String
        mNmhAdVrLoDmLYjv = OSSClient(endpoint: OXknxPAOlgZvdwIV, credentialProvider: OSSStsTokenCredentialProvider(accessKeyId: qiFnpqkgnIMowCCr, secretKeyId: LSCrLFneQOQTFpEp, securityToken: MgMtRBePMHRwscie))
    }
    
    func kDfxkBtFUkZRElku(amwLsSsqearISEaM: String, yGxCIvPKkYdnsEub: String, hkyWgyVaDaXtTPUq: ((_ hyridvHtGRONajzD: Int, _ rzZhbifyokYykTwb: Int) -> Void)? = nil, WlyMgrlTDrLWiMcu: ((_ mKUIzAxlvBGUVfVk: Bool, _ nYmOYAxJcugyWBBX: String?, _ QWgDujAUvfAGYENs: String?) -> Void)? = nil) {
        if mNmhAdVrLoDmLYjv == nil {
            WlyMgrlTDrLWiMcu?(false, nil, "can not upload file")
            return
        }
        let FJDodihcWRhErEJp = (Double((try! FileManager.default.attributesOfItem(atPath: amwLsSsqearISEaM))[.size] as! UInt64)) / 1024.0
        let ulLrzuIEdnTDhAtR = FJDodihcWRhErEJp < 1 ? 1 : Int(FJDodihcWRhErEJp)
        NbDnvbpESqnqDInl.ZSgCfzHnxpKhZfxO.etDatGyKwukGrbHv(kEZOdhLcNmJFYywX: "prepareFileUpload", EeqbYNIBaZfkqYaF: [
            "sizeKB": ulLrzuIEdnTDhAtR,
        ]) { mKUIzAxlvBGUVfVk, EyTIVrNCTVqmYwPu, VnJfdRrzZmdnJmsc, EmqetgaKELbCTYMP in
            if !mKUIzAxlvBGUVfVk {
                WlyMgrlTDrLWiMcu?(false, nil, EmqetgaKELbCTYMP)
                return
            }
            let JbUdhLkKLwhgqnDO = OSSPutObjectRequest()
            JbUdhLkKLwhgqnDO.bucketName = self.raHxkMJGVZaVKyRg
            JbUdhLkKLwhgqnDO.objectKey = "client/\(yGxCIvPKkYdnsEub)/ios_\(UUID().uuidString).\(amwLsSsqearISEaM.split(separator: "/").last!.split(separator: ".").last!)"
            JbUdhLkKLwhgqnDO.uploadingFileURL = URL(string: amwLsSsqearISEaM)
            JbUdhLkKLwhgqnDO.uploadProgress = { yQmkuvzBlqMskBpb, ipNgPWFCcGcLvwgJ, dwAgNHbdRTlJiUzC in
                hkyWgyVaDaXtTPUq?(Int(ipNgPWFCcGcLvwgJ), Int(ipNgPWFCcGcLvwgJ + dwAgNHbdRTlJiUzC))
            }
            JbUdhLkKLwhgqnDO.objectMeta!["x-oss-forbid-overwrite"] = "true"
            JbUdhLkKLwhgqnDO.objectMeta!["x-oss-object-acl"] = "public-read"
            JbUdhLkKLwhgqnDO.objectMeta!["x-oss-storage-class"] = "Standard"
            self.mNmhAdVrLoDmLYjv!.putObject(JbUdhLkKLwhgqnDO).continue({ nEnqbcOcHDqizISE in
                if nEnqbcOcHDqizISE.error != nil {
                    WlyMgrlTDrLWiMcu?(false, nil, nEnqbcOcHDqizISE.error!.localizedDescription)
                    return
                }
                let nYmOYAxJcugyWBBX = self.xdddztrOGitTYeMv != nil ? "\(self.xdddztrOGitTYeMv!)/\(JbUdhLkKLwhgqnDO.objectKey)" : "https://\(JbUdhLkKLwhgqnDO.bucketName).\(self.mNmhAdVrLoDmLYjv!.endpoint.components(separatedBy: "://").last!)/\(JbUdhLkKLwhgqnDO.objectKey)"
                NbDnvbpESqnqDInl.ZSgCfzHnxpKhZfxO.etDatGyKwukGrbHv(kEZOdhLcNmJFYywX: "finishFileUpload", EeqbYNIBaZfkqYaF: [
                    "url": nYmOYAxJcugyWBBX,
                ]) { mKUIzAxlvBGUVfVk, EyTIVrNCTVqmYwPu, VnJfdRrzZmdnJmsc, EmqetgaKELbCTYMP in
                    if !mKUIzAxlvBGUVfVk {
                        WlyMgrlTDrLWiMcu?(false, nil, EmqetgaKELbCTYMP)
                        return
                    }
                    WlyMgrlTDrLWiMcu?(true, nYmOYAxJcugyWBBX, nil)
                }
                return
            })
        }
    }
    
    func GOyxLapaPKzDshmz(BrunOZpLCJfGfiVp: String, oLSqsrrceEAsoUqe: Int = 0) -> String {
        return "\(BrunOZpLCJfGfiVp)?x-oss-process=video/snapshot,w_\(oLSqsrrceEAsoUqe),t_1000,m_fast,f_jpg,ar_auto"
    }
    
    func MUAUTfkBXGZRgvdj(ADdKspuycARtinOa: String, dfkSNcxBIoSWosmM: Int = 1080, HFEAsoQlmBFobefu: Int = 70) -> String {
        let nWLtCfwIuAXjqEWi = ADdKspuycARtinOa.split(separator: "?").first!
        let jYXFVAjWzPaXTTJP = nWLtCfwIuAXjqEWi.split(separator: ",").last?.uppercased()
        if ["JPG", "JPEG", "WEBP"].contains(jYXFVAjWzPaXTTJP) {
            return "\(nWLtCfwIuAXjqEWi)?x-oss-process=image/resize,m_lfit,limit_1,l_\(dfkSNcxBIoSWosmM)/quality,Q_\(HFEAsoQlmBFobefu)"
        } else if ["PNG", "GIF"].contains(jYXFVAjWzPaXTTJP) {
            return "\(nWLtCfwIuAXjqEWi)?x-oss-process=image/resize,m_lfit,limit_1,l_\(dfkSNcxBIoSWosmM)"
        } else {
            return ADdKspuycARtinOa
        }
    }
}
